﻿using System.Xml.Linq;
using IDeal.Extensions;
using IDeal.Models;
using IDeal.Services;

namespace IDeal.Serialization
{
    internal class StatusRequestXmlWriter : XmlMessageWriter<StatusRequest>
    {
        public StatusRequestXmlWriter(IIDealServices iDealServices)
            : base(iDealServices)
        {
        }

        protected override string RootElementName
        {
            get { return "AcquirerStatusReq"; }
        }

        protected override string GetHashSource(StatusRequest instance)
        {
            return
                instance.CreatedDateTimeStamp.ToIDealFormat() +
                instance.MerchantId +
                instance.SubId +
                instance.TransactionId;
        }

        protected override void SetupRootElement(StatusRequest instance, XElement rootElement)
        {
            var signature = CreateSignature(instance);

            rootElement.Add(new XElement
            (
                Ns + "Merchant",
                new XElement(Ns + "merchantID", instance.MerchantId),
                new XElement(Ns + "subID", instance.SubId),
                new XElement(Ns + "authentication", "SHA1_RSA"),
                new XElement(Ns + "token", signature.Fingerprint),
                new XElement(Ns + "tokenCode", signature.Token)
            ));

            rootElement.Add(new XElement
            (
                Ns + "Transaction",
                new XElement(Ns + "transactionID", instance.TransactionId)
            ));
        }
    }
}
